package com.ai.vita.redis.controller;

import com.ai.vita.redis.client.RedisClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 首页.
 */
@Controller
public class IndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisClient redisClient;

    /**
     * 根目录.
     * @return
     */
    @RequestMapping("/")
    public String index(){
        return "index/index";
    }

    @RequestMapping("/initLink")
    @ResponseBody
    public String initLink(HttpServletRequest request){
        String token = request.getParameter("token");
        String host = request.getParameter("host");
        String[] hosts = host.split(",");
        String ip = hosts[1];
        String port = hosts[2];
        String auth = null;
        if(hosts.length >= 4){
            auth = hosts[3];
        }
        return redisClient.init(token, ip, port, auth);
    }

    /**
     * 获取redis的db信息.
     * @param request 请求
     * @return redis db信息
     */
    @RequestMapping(value = "/getDbs")
    @ResponseBody
    public String getDbs(HttpServletRequest request){
        String clientName = request.getParameter("clientName");
        return JSONArray.fromObject(redisClient.getDbs(clientName)).toString();
    }

    /**
     * 查询key.
     * @param request 请求信息
     * @return 所有db里匹配的key值
     */
    @RequestMapping(value = "/queryKey")
    @ResponseBody
    public String queryKey(HttpServletRequest request){
        String key = request.getParameter("key");
        String clientName = request.getParameter("clientName");
        return JSONArray.fromObject(redisClient.queryKey(clientName, key)).toString();
    }

    /**
     * 获取指定db的keys.
     * @param request 请求信息
     * @return 获取某个db的所有key
     */
    @RequestMapping(value = "/getDbKeys")
    @ResponseBody
    public String getDbKeys(HttpServletRequest request){
        String dbIndex = request.getParameter("dbIndex");
        String clientName = request.getParameter("clientName");
        return JSONArray.fromObject(redisClient.getKeys(clientName, Integer.parseInt(dbIndex))).toString();
    }

    /**
     * 根据key取值.
     * @param request 请求信息
     * @return 某个db里某个key的值
     */
    @RequestMapping(value = "/getValue")
    @ResponseBody
    public String getValue(HttpServletRequest request){
        String dbIndex = request.getParameter("dbIndex");
        String key = request.getParameter("key");
        String clientName = request.getParameter("clientName");
        return JSONObject.fromObject(redisClient.getValue(clientName, Integer.parseInt(dbIndex), key)).toString();
    }

    /**
     * 新增key.
     * @param request 请求信息
     * @return 新增结果
     */
    @RequestMapping(value = "/addKey")
    @ResponseBody
    public String addKey(HttpServletRequest request){
        String clientName = request.getParameter("clientName");
        String dbIndex = request.getParameter("dbIndex");
        String key = request.getParameter("key");
        String valueType = request.getParameter("valueType");
        String value = request.getParameter("value");
        return "" + redisClient.addKey(clientName, Integer.parseInt(dbIndex), key, valueType, value);
    }

    /**
     * 删除key.
     * @param request 请求信息
     * @return 删除结果
     */
    @RequestMapping(value = "/deleteKey")
    @ResponseBody
    public String deleteKey(HttpServletRequest request){
        String clientName = request.getParameter("clientName");
        String dbIndex = request.getParameter("dbIndex");
        String key = request.getParameter("key");
        return "" + redisClient.deleteKey(clientName, Integer.parseInt(dbIndex), key);
    }

    /**
     * 更新key.
     * @param request 请求信息
     * @return 更新结果
     */
    @RequestMapping(value = "/updateKey")
    @ResponseBody
    public String updateKey(HttpServletRequest request){
        String clientName = request.getParameter("clientName");
        String dbIndex = request.getParameter("dbIndex");
        String key = request.getParameter("key");
        String valueType = request.getParameter("valueType");
        String value = request.getParameter("value");
        redisClient.deleteKey(clientName, Integer.parseInt(dbIndex), key);
        return "" + redisClient.addKey(clientName, Integer.parseInt(dbIndex), key, valueType, value);
    }

    /**
     * 是否存在key.
     * @param request 请求信息
     * @return 结果
     */
    @RequestMapping(value = "/exists")
    @ResponseBody
    public String exists(HttpServletRequest request){
        String clientName = request.getParameter("clientName");
        String dbIndex = request.getParameter("dbIndex");
        String key = request.getParameter("key");
        return "" + redisClient.existsKey(clientName, Integer.parseInt(dbIndex), key);
    }

}
